//
//  PMPolygonShape.m
//  PezMonkey
//
//  Created by Spence DiNicolantonio on 10/29/09.
//  Copyright 2009 Necrosoft. All rights reserved.
//

/**
 * A subclass of PMShape for representing convex polygon collision shapes. The 
 * PMPolygonShape must be defined with clockwise winding.
 */

#import "PMPolygonShape.h"
#import "PMBody.h"


@implementation PMPolygonShape


#pragma mark -
#pragma mark Init

/**
 * Returns a PMPolygonShape object initialized to correspond to a given PMBody 
 * object, constructed using a given set of vertices relative to the body's center
 * of gravity. The given vertices must define a convex polygon with clockwise 
 * winding. An assertion will be thrown if this is not the case.
 */
- (id)initWithBody:(PMBody *)aBody vertexArray:(PMVect[])vertices {
	return [self initWithBody:aBody center:PMVectZero vertexArray:vertices];
}

/**
 * Returns a PMPolygonShape object initialized to correspond to a given PMBody 
 * object, constructed using a given set of vertices relative to a given center
 * point offset from the body's center of gravity. The given vertices must define 
 * a convex polygon with clockwise winding. An assertion will be thrown if this 
 * is not the case.
 */
- (id)initWithBody:(PMBody *)aBody center:(PMVect)center vertexArray:(PMVect[])vertices {
	// get number of vertices 
	int vertCount = PMVectCount(vertices);
	
	// allocate and initialize cpShape
	shape = cpPolyShapeNew(aBody.cpBody, vertCount, vertices, center);
	self.body = aBody;
	
	return self;
}


#pragma mark -
#pragma mark Getters

/**
 * Returns the number of vertices that make up the receiver.
 */
- (NSUInteger)vertexCount {
	return ((cpPolyShape *) shape)->numVerts;
}

/**
 * Returns a pointer to an array of the receiver's vertices. These vertices should
 * never be altered in any way. This method should be used for reference only.
 */
- (PMVect *)vertices {
	return (PMVect *) ((cpPolyShape *) shape)->verts;
}

/**
 * Returns the receiver's vertex located at a given index. For example, if the
 * receiver is a square (four vertices) passing an index value 2 will give return
 * the 3rd vertex originaly given in the receiver's construction.
 */
- (PMVect)vertexAtIndex:(NSInteger)index {
	return (PMVect) ((cpPolyShape *) shape)->verts[index];
}

@end
